import os

def get_folder_size(folder_path):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(folder_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            if not os.path.islink(fp):
                try:
                    total_size += os.path.getsize(fp)
                except FileNotFoundError:
                    pass
    return total_size

def list_folder_sizes_sorted(directory='.'):
    print(f"扫描目录: {os.path.abspath(directory)}")
    print("{:<60} {:<15}".format("文件夹名", "大小"))
    print("-" * 80)
    
    # 收集所有文件夹及其大小
    folders = []
    for item in os.listdir(directory):
        item_path = os.path.join(directory, item)
        if os.path.isdir(item_path):
            size_bytes = get_folder_size(item_path)
            folders.append((item, size_bytes))
    
    # 按大小排序（从大到小）
    folders.sort(key=lambda x: x[1], reverse=True)
    
    # 打印排序后的结果
    for folder, size_bytes in folders:
        size_kb = size_bytes / 1024
        size_mb = size_kb / 1024
        size_gb = size_mb / 1024
        
        if size_gb >= 1:
            size_str = f"{size_gb:.2f} GB"
        elif size_mb >= 1:
            size_str = f"{size_mb:.2f} MB"
        elif size_kb >= 1:
            size_str = f"{size_kb:.2f} KB"
        else:
            size_str = f"{size_bytes} bytes"
        
        print("{:<60} {:<15}".format(folder, size_str))

if __name__ == "__main__":
    list_folder_sizes_sorted(r"C:\Users\shuai\AppData\Local")